<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_commandes = valueExists((int)$_GET['id'], "CODECOMMANDEINTERNE", "commande_interne", $connexion);
			
			if($nb_commandes == 1){ // la commande à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner une seule commande
	- La commande doit exister dans la BDD

*****************************************/

	// récupération des informations de la commande
	$query_recuperation_commande = "SELECT ca.DATECOMMANDE AS date, ca.CONVENTIONSIGNEE AS convention, ca.COMMENTAIRE AS commentaire, ca.CODEPERSONNE AS idPersonne, pe.NOMPERSONNE AS nom, pe.PRENOMPERSONNE AS prenom, ca.CODESTRUCTURE AS idStructure, s.CODEJB as numcor, s.NOMSTRUCTURE AS structure, s.ADRESSESTRUCTURE AS adresse, cu.NOMCOMMUNE AS commune, cu.CODEPOSTAL AS cp, pa.NOMPAYS AS pays, lc.CODETAXON AS idTaxon, ll.CODELIGNELIVRAISON AS idLigneLivraison, pb.NOMPROBLEME AS probleme, lc.QUANTITECOMMANDEE AS quantiteCommandee, ll.QUANTITELIVREE AS quantiteLivree, fam.NOMFAMILLE AS famille, gen.NOMGENRE AS genre, esp.NOMESPECE AS espece, sesp.NOMSOUSESPECE AS sousespece, var.NOMVARIETE AS variete, sa.CODESACHETGRAINE AS idSachet, sa.REFERENCESACHETGRAINES AS refSachet, se.CODESEMIS AS idSemis, se.REFERENCESEMIS AS refSemis, tou.CODETOUFFE AS idTouffe, tou.REFERENCETOUFFE AS refTouffe ";
	$query_recuperation_commande .= "FROM commande_interne ca ";
	$query_recuperation_commande .= "INNER JOIN personne pe ON pe.CODEPERSONNE = ca.CODEPERSONNE ";
	$query_recuperation_commande .= "INNER JOIN structure s ON s.CODESTRUCTURE = ca.CODESTRUCTURE ";
	$query_recuperation_commande .= "LEFT JOIN commune cu ON cu.CODECOMMUNE = s.CODECOMMUNE ";
	$query_recuperation_commande .= "LEFT JOIN pays pa ON pa.CODEPAYS = cu.CODEPAYS ";
	$query_recuperation_commande .= "LEFT JOIN ligne_commande lc ON lc.CODECOMMANDEINTERNE = ca.CODECOMMANDEINTERNE ";
	$query_recuperation_commande .= "LEFT JOIN ligne_livraison ll ON ll.CODELIGNECOMMANDE = lc.CODELIGNECOMMANDE ";
	$query_recuperation_commande .= "LEFT JOIN entree en ON en.CODELIGNELIVRAISON = ll.CODELIGNELIVRAISON ";
	$query_recuperation_commande .= "LEFT JOIN sachet_graines sa ON en.SAC_CODESACHETGRAINE = sa.CODESACHETGRAINE ";
	$query_recuperation_commande .= "LEFT JOIN semis se ON en.SEM_CODESEMIS = se.CODESEMIS ";
	$query_recuperation_commande .= "LEFT JOIN touffe tou ON en.TOU_CODETOUFFE = tou.CODETOUFFE ";
	$query_recuperation_commande .= "LEFT JOIN probleme pb ON pb.CODEPROBLEME = ll.CODEPROBLEME ";
	$query_recuperation_commande .= "LEFT JOIN taxon t ON t.CODETAXON = lc.CODETAXON ";
	$query_recuperation_commande .= "LEFT JOIN est_de_famille l1 INNER JOIN famille fam ON fam.CODEFAMILLE = l1.CODEFAMILLE ON l1.CODETAXON = t.CODETAXON AND l1.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN appartient_a_genre l2 INNER JOIN genre gen ON gen.CODEGENRE = l2.CODEGENRE ON l2.CODETAXON = t.CODETAXON AND l2.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN est_de_espece l3 INNER JOIN espece esp ON esp.CODEESPECE = l3.CODEESPECE ON l3.CODETAXON = t.CODETAXON AND l3.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN est_de_sous_espece l33 INNER JOIN sous_espece sesp ON sesp.CODESOUSESPECE = l33.CODESOUSESPECE ON l33.CODETAXON = t.CODETAXON AND l33.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "LEFT JOIN a_comme_variete l4 INNER JOIN variete var ON var.CODEVARIETE = l4.CODEVARIETE ON l4.CODETAXON = t.CODETAXON AND l4.CODECLASSIFICATION = t.CODECLASSIFICATION ";
	$query_recuperation_commande .= "WHERE ca.CODECOMMANDEINTERNE = ".$_GET['id']." ";
	$query_recuperation_commande .= "ORDER BY famille, genre, espece, variete ";
	
	$result_recuperation_commande = mysql_query($query_recuperation_commande, $connexion) or logError("RECUPERATION COMMANDE INTERNE-".$query_recuperation_commande."-".mysql_error());
	
	$commande = array(); // on créé un tableau contenant toutes les informations de la commande
	while($tab_recuperation_commande = mysql_fetch_assoc($result_recuperation_commande)){
	
		$commande['date'] = $tab_recuperation_commande['date'];
		$commande['convention'] = (bool)$tab_recuperation_commande['convention'];
		$commande['commentaire'] = $tab_recuperation_commande['commentaire'];
		$commande['structure']['id'] = $tab_recuperation_commande['idStructure'];
		$commande['structure']['nom'] = $tab_recuperation_commande['structure'];
		$commande['structure']['adresse'] = $tab_recuperation_commande['adresse'];
		$commande['structure']['commune'] = $tab_recuperation_commande['commune'];
		$commande['structure']['cp'] = $tab_recuperation_commande['cp'];
		$commande['structure']['pays'] = $tab_recuperation_commande['pays'];
		$commande['personne']['id'] = $tab_recuperation_commande['idPersonne'];
		$commande['personne']['nom'] = $tab_recuperation_commande['nom'];
		$commande['personne']['prenom'] = $tab_recuperation_commande['prenom'];
		
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['id'] = $tab_recuperation_commande['idTaxon'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['livre'] = !is_null($tab_recuperation_commande['idLigneLivraison']);
		if(!is_null($tab_recuperation_commande['probleme'])){
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['probleme'] = $tab_recuperation_commande['probleme'];
		}
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['quantiteCommandee'] = $tab_recuperation_commande['quantiteCommandee'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['quantiteLivree'] = $tab_recuperation_commande['quantiteLivree'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['famille'] = $tab_recuperation_commande['famille'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['genre'] = $tab_recuperation_commande['genre'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['espece'] = $tab_recuperation_commande['espece'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['sousespece'] = $tab_recuperation_commande['sousespece'];
		$commande['taxons'][$tab_recuperation_commande['idTaxon']]['variete'] = $tab_recuperation_commande['variete'];
		
		if(!is_null($tab_recuperation_commande['idSachet'])){
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['sachet']['id'] = $tab_recuperation_commande['idSachet'];
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['sachet']['reference'] = $tab_recuperation_commande['refSachet'];
		}
		if(!is_null($tab_recuperation_commande['idSemis'])){
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['semis']['id'] = $tab_recuperation_commande['idSemis'];
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['semis']['reference'] = $tab_recuperation_commande['refSemis'];
		}
		if(!is_null($tab_recuperation_commande['idTouffe'])){
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['touffe']['id'] = $tab_recuperation_commande['idTouffe'];
			$commande['taxons'][$tab_recuperation_commande['idTaxon']]['touffe']['reference'] = $tab_recuperation_commande['refTouffe'];
		}
	
	}
	
	mysql_free_result($result_recuperation_commande);
	
?>
<h3>Détail d'une commande</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td colspan="2">
			Commande du <b><?php echo dateFr($commande['date']); ?></b> à <b><?php echo htmlentities($commande['structure']['nom'], ENT_NOQUOTES, "UTF-8"); ?></b>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations</th>
	</tr>
	<tr>
		<td style="width: 20%;" class="etiquette">A:</td>
		<td style="width: 80%;">
			<?php echo "<a href=\"popup.php?p=gestion/structure_affiche_popup&amp;id=".$commande['structure']['id']."\" onclick=\"popup(this.href, 400, 500); return false;\">".htmlentities($commande['structure']['nom'], ENT_NOQUOTES, "UTF-8")."</a>"; ?><br />
			<?php echo htmlentities($commande['structure']['adresse'], ENT_NOQUOTES, "UTF-8"); ?><br />
			<?php echo htmlentities($commande['structure']['cp'], ENT_NOQUOTES, "UTF-8"); ?> - <?php echo htmlentities($commande['structure']['commune'], ENT_NOQUOTES, "UTF-8"); ?> (<?php echo htmlentities(mb_strtoupper($commande['structure']['pays']), ENT_NOQUOTES, "UTF-8"); ?>)
		</td>
	</tr>
	<tr>
		<td class="etiquette">Pour:</td>
		<td>
			<?php echo "<a href=\"popup.php?p=gestion/personne_affiche_popup&amp;id=".$commande['personne']['id']."\" onclick=\"popup(this.href, 350, 400); return false;\">".htmlentities(capitalise($commande['personne']['prenom']), ENT_NOQUOTES, "UTF-8")." ".htmlentities(mb_strtoupper($commande['personne']['nom']), ENT_NOQUOTES, "UTF-8")."</a>"; ?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Conv. signée:</td>
		<td><img src="images/<?php echo boolToText((bool)$commande['convention']); ?>.png" alt="<?php echo boolToText((bool)$commande['convention']); ?>" /></td>
	</tr>
	<tr>
		<th colspan="2">Contenu</th>
	</tr>
	<tr>
		<td colspan="2">
			<ul>
				<?php
					foreach($commande['taxons'] as $taxon){
						echo "<li>";
						if($taxon['livre']){
							if(isset($taxon['probleme'])){
								echo "<img src=\"images/non.png\" alt=\"Problème\" title=\"Problème pour la livraison\" /> <span style=\"color: #FF0000\">".htmlentities($taxon['probleme'], ENT_NOQUOTES, "UTF-8")."</span>";
							}else{
								echo "<img src=\"images/oui.png\" alt=\"Livré\" title=\"Livré\" />";
							}
						}else{
							echo "<img src=\"images/inconnu.png\" alt=\"Attente\" title=\"Attente de livraison\" />";
						}
						echo " - <a href=\"popup.php?p=gestion/taxon_affiche_popup&amp;id=".$taxon['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">(".htmlentities(capitalise($taxon['famille']), ENT_NOQUOTES, "UTF-8").") <b>".htmlentities(capitalise($taxon['genre']), ENT_NOQUOTES, "UTF-8")." ".htmlentities($taxon['espece'], ENT_NOQUOTES, "UTF-8")." ".htmlentities($taxon['sousespece'], ENT_NOQUOTES, "UTF-8")."</b> <i>".htmlentities($taxon['variete'], ENT_NOQUOTES, "UTF-8")."</i></a>";
						if(!is_null($taxon['quantiteCommandee'])){
							echo " (commandés: ".$taxon['quantiteCommandee'].")";
						}
						if(isset($taxon['sachet'])){
							echo " - [<a href=\"popup.php?p=gestion/sachetgraines_affiche_popup&amp;id=".$taxon['sachet']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Sachet";
							if(!is_null($taxon['sachet']['reference'])){
								echo " (n° ent. ".htmlentities($taxon['sachet']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a>]";
						}else if(isset($taxon['semis'])){
							echo " - [<a href=\"popup.php?p=gestion/semis_affiche_popup&amp;id=".$taxon['semis']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Semis";
							if(!is_null($taxon['semis']['reference'])){
								echo " (n° ent. ".htmlentities($taxon['semis']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a>]";
						}else if(isset($taxon['touffe'])){
							echo " - [<a href=\"popup.php?p=gestion/touffe_affiche_popup&amp;id=".$taxon['touffe']['id']."\" onclick=\"popup(this.href, 640, 480); return false;\">Touffe";
							if(!is_null($taxon['touffe']['reference'])){
								echo " (n° ent. ".htmlentities($taxon['touffe']['reference'], ENT_NOQUOTES, "UTF-8").")";
							}
							echo "</a>]";
						}
						if(!is_null($taxon['quantiteLivree'])){
							echo " (livrés: ".$taxon['quantiteLivree'].")";
						}
						echo "</li>";
					}
					unset($taxon);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2"><?php echo nl2br(htmlentities($commande['commentaire'], ENT_NOQUOTES, "UTF-8")); ?></td>
	</tr>
</table>
<div class="information">
	<div class="titre">Légende</div>
	<p>
		<b>Contenu :</b><br />
		<img src="images/oui.png" alt="oui" /> : livré &nbsp;&nbsp;-&nbsp;&nbsp; <img src="images/non.png" alt="non" /> : problème pour la livraison &nbsp;&nbsp;-&nbsp;&nbsp; <img src="images/inconnu.png" alt="non" /> : en attente de livraison
	</p>
</div>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("La commande n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("La commande n'est pas ou mal indiquée", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>